<?php

namespace app\store\controller\gifts;
use app\store\controller\Controller;
use app\store\model\Gifts as GiftsModel;
use app\store\model\Wxapp as WxappModel;

/**
 * 爆品礼物管理控制器
 * Class Goods
 * @package app\store\controller
 */
class Gifts extends Controller
{
    /**
     * 爆品礼物
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        // 获取全部商品列表
        $model = new GiftsModel;
        $list = $model->getList(array_merge(['status' => -1], $this->request->param()));
        return $this->fetch('index', compact('list'));
    }

    /**
     * 添加商品
     * @return array|mixed
     * @throws \think\exception\PDOException
     */
    public function add()
    {
        $wxapp_id = $this::getWxappId();
        //获取分销方案
        $dealerCase = WxappModel::dealerCase($wxapp_id);

        if (!$this->request->isAjax()) {
            return $this->fetch(
                'add',
                ['dealerCase'=>$dealerCase]
            );
        }
        $model = new GiftsModel;
        if ($model->add($this->postData('gifts'))) {
            return $this->renderSuccess('添加成功', url('gifts.gifts/index'));
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }



    /**
     * 商品编辑
     * @param $gifts_id
     * @return array|bool|mixed
     */
    public function edit($gifts_id)
    {

        $wxapp_id = $this::getWxappId();
        //获取分销方案
        $dealerCase = WxappModel::dealerCase($wxapp_id);
        // 商品详情
        $model = GiftsModel::detail($gifts_id);

        if (!$this->request->isAjax()) {

            return $this->fetch(
                'edit',
                compact('model','dealerCase')
            );
        }

        // 更新记录
        if ($model->edit($this->postData('gifts'))) {
            return $this->renderSuccess('更新成功', url('gifts.gifts/index'));
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 修改商品状态
     * @param $gifts_id
     * @param boolean $state
     * @return array
     */
    public function state($gifts_id, $state)
    {
        // 商品详情
        $model = GiftsModel::detail($gifts_id);
        if (!$model->setStatus($state)) {
            return $this->renderError('操作失败');
        }
        return $this->renderSuccess('操作成功');
    }

    /**
     * 删除商品
     * @param $gifts_id
     * @return array
     */
    public function delete($gifts_id)
    {
        // 商品详情
        $model = GiftsModel::detail($gifts_id);
        if (!$model->setDelete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }



    /**
     * 爆品排名
     * @param $gifts_id
     * @return array
     */
    public function ranking()
    {

        $param = $this->request->param();
        // 商品列表获取条件
        $params = array_merge([
            'search' => '',         // 搜索关键词
            'listRows' => 15,       // 每页数量
        ], $param);

        // 筛选条件
        $filter = [];

        !empty($params['search']) && $filter['c.nickName|c.mobile'] = ['like', '%' . trim($params['search']) . '%'];

        // 商品详情
        $model = GiftsModel::detail($params['gifts_id']);

        if(!$model){
            return $this->renderError('爆品礼不存在');
        }

        //查询订单表
        $list = db('gifts_order')
            ->alias('a')
            ->join('yoshop_gifts_order_goods b','a.order_id=b.order_id')
            ->join('yoshop_user c','a.superior_user_id=c.user_id')
            ->where(['a.wxapp_id'=>$this->getWxappId(),'a.is_delete'=>0,'a.superior_user_id'=>['<>',0],'b.gifts_id'=>$params['gifts_id'],'a.pay_status'=>20])
            ->where($filter)
            ->group('a.superior_user_id')
            ->field([
                'count(a.superior_user_id)' => 'total',
                'a.superior_user_id' => 'superior_user_id',
                'c.user_id'=>'user_id',
                'c.nickName'=>'nickName',
                'c.avatarUrl'=>'avatarUrl',
                'b.gifts_title'=>'gifts_title',
                'c.mobile'=>'mobile',

            ])
            ->order('total desc')->paginate($params['listRows'], false, [
                'query' => \request()->request()
            ])->each(function($item,$key){

                $item['ID'] = $key+1;
                return $item;
            });

        $gifts_id = $params['gifts_id'];

        return $this->fetch('ranking', compact('list','gifts_id'));

    }


    /**
     * 订单导出
     * @param string $dataType
     * @throws \think\exception\DbException
     */
    public function export()
    {

        $model = new GiftsModel;
        if (!$model->exportList($this->request->param())) {
            return $this->renderError($model->getError() ?: '操作失败');
        }
        return $model->exportList($this->request->param());
    }

}
